<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogAddOrEdit.visible"
    :width="900"
    :title="dialogAddOrEdit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-form ref="addOrEditFormRef" v-loading="loading" :model="formData" :rules="rules" label-width="160" size="">
      <el-row justify="start" class="d_left">
        <el-col :span="12" class="">
          <el-form-item label="账户编号" prop="code">
            <el-input v-model="formData.code" disabled placeholder="自动生成" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="账户名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入账户名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="工资保证金开户银行" prop="bankName">
            <!-- <el-input v-model="formData.bankName" placeholder="请输入工资保证金开户银行" /> -->
            <el-select v-model="formData.bankName" placeholder="请选择">
              <el-option v-for="item in bankNameList" :key="item.label" :label="item.label" :value="item.label" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="">
          <el-form-item label="工资保证金银行账户" prop="bankNo">
            <el-input v-model="formData.bankNo" placeholder="请输入工资保证金银行账户" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="归属人社名称" prop="rsName">
            <el-input v-model="formData.rsName" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAddOrEdit.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ProjectGuaranteeAccountEditForm } from "@/api/guarantee/types";
import {
  CreateProjectGuaranteeAccount,
  UpdateProjectGuaranteeAccount,
  GetProjectGuaranteeAccount,
} from "@/api/guarantee/index";
import { getDicts } from "@/store/modules/dict";
import { FormRules } from "element-plus";

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "创建保证金账户",
      footBtn: "核实账户信息",
      id: 0,
    }),
  },
});

const bankNameList = getDicts("bankTypeTypes");
const { dialogAddOrEdit } = toRefs(props);

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
const formData = reactive<ProjectGuaranteeAccountEditForm>({});
const addOrEditFormRef = ref(ElForm);

const rules = reactive<FormRules<ProjectGuaranteeAccountEditForm>>({
  name: [{ required: true, message: "请输入账户名称", trigger: "blur" }],
  bankName: [{ required: true, message: "请输入工资保证金开户银行", trigger: "blur" }],
  bankNo: [{ required: true, message: "请输入工资保证金银行账户", trigger: "blur" }],
});

onMounted(() => {
  loading.value = true;
  let id = 0;
  if (dialogAddOrEdit.value.id) {
    id = dialogAddOrEdit.value.id;
  }
  GetProjectGuaranteeAccount(id)
    .then((res) => {
      Object.assign(formData, res.data);
    })
    .finally(() => {
      loading.value = false;
    });
});
/**
 * 关闭弹窗
 */
const closeDialog = (query: boolean, data?: any) => {
  emit("sendClose", query, data);
  resetForm();
};

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      formData.type = 1; //创建保证金账户
      formData.attachments = [];
      if (dialogAddOrEdit.value.id) {
        UpdateProjectGuaranteeAccount(formData)
          .then((res) => {
            if (res.success) {
              closeDialog(true);
            }
          })
          .finally(() => (loading.value = false));
        return;
      }
      CreateProjectGuaranteeAccount(formData)
        .then((res) => {
          if (res.success) {
            closeDialog(true);
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
